package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏业务:geofenceQuery={}",geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        /* 需要给每一个围栏VO对象补充车辆数据:绑定车辆总数 可用车辆数 */
        for(int i = 0; i < list.size(); i++){
            //取出当前遍历到的围栏VO
            GeofenceVO geofenceVO = list.get(i);
            //封装车辆查询对象,查出当前围栏上绑定的车辆列表
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //获取当前围栏绑定的车辆总数
            int totalNum = vehicleVOList.size();
            //定义变量保存可用车辆数
            int availableNum = 0;
            //遍历每一辆车,如果车辆状态为1(空闲),可用车辆数++
            for(VehicleVO vehicleVO : vehicleVOList){
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            geofenceVO.setAvailableNum(availableNum);//将可用车辆数赋值给围栏VO
            geofenceVO.setTotalNum(totalNum);//将车辆总数赋值给围栏VO
            //因为后续要查看当前围栏上所有车辆的详细信息,所以把查出来的车辆列表也赋值给围栏
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("新增电子围栏业务:geofenceParam={}",geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        if(geofence.getId()!=null){//更新
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }else{//新增
            geofence.setStatus("1");//新增的围栏默认都是启用状态
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除电子围栏业务:id={}",id);
        //业务:需要判断当前围栏上是否有绑定的车辆,如果有绑定车辆,就不能删除!
        //1.封装车辆查询对象,查询该围栏下的所有绑定车辆
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //2.对查询结果进行判断
        if(list.size()>0 && list!=null){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else{
            //3.只有没有绑定车辆时,才能删除围栏
            geofenceMapper.deleteById(id);
        }
    }
}
